package com.apes.framework.plugin.condition;

import com.apes.framework.plugin.condition.model.ConditionTypes;
import com.apes.framework.plugin.condition.model.PricingProcedure;
import com.apes.framework.plugin.condition.model.PricingProcedureItem;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：条件执行上下文
 *
 * @author xul
 * @create 2018-11-29 11:03
 */
@Data
public class PricingProcedureContext {

    /**
     * 传入参数
     */
    private final Map<String, ?> parameters;

    /**
     * 输出参数
     */
    private final Map<String, Object> out = new HashMap<>();

    /**
     * 执行结果
     */
    private final Map<String, Object> result = new HashMap<>();

    /**
     * 执行过程列表
     */
    private final List<Map> procedures = new ArrayList<>();

    /**
     * 当前执行过程
     */
    private Map currentProcedure;

    public Map getCurrentProcedure() {
        if (currentProcedure == null) currentProcedure = new HashMap();
        return currentProcedure;
    }

    public void addProcedure(Map procedure) {
        procedures.add(procedure);
    }

    public PricingProcedureContext(Map parameters) {
        this.parameters = parameters;
    }

    public <T> T getParameter(String name) {
        return (T) parameters.get(name);
    }

    public String getParameterStr(String name) {
        Object v = getParameter(name);
        if(v instanceof BigDecimal) return v.toString();
        if(v instanceof String) return (String) v;
        if(v instanceof Double) v.toString();
        return String.valueOf(v);
    }

    public Double getParameterDouble(String name) {
        return Double.valueOf(getParameterStr(name));
    }

    /**
     * 计算过程
     **/
    public void setPricingProcedure(PricingProcedure pricingProcedure) {
        getCurrentProcedure().put("pricingProcedure", pricingProcedure);
    }

    public PricingProcedure getPricingProcedure() {
        return getCurrentProcedureValue("pricingProcedure");
    }

    /**
     * 计算过程明细
     **/
    public void setPricingProcedureItem(PricingProcedureItem procedureItem) {
        getCurrentProcedure().put("pricingProcedureItem", procedureItem);
    }

    public PricingProcedureItem getPricingProcedureItem() {
        return getCurrentProcedureValue("pricingProcedureItem");
    }

    /**
     * 条件类型
     **/
    public void setConditionTypes(ConditionTypes conditionTypes) {
        getCurrentProcedure().put("conditionTypes", conditionTypes);
    }

    public ConditionTypes getConditionTypes() {
        return getCurrentProcedureValue("conditionTypes");
    }

    /**
     * 价格
     **/
    public void setPrice(Double price) {
        getCurrentProcedure().put("price", price);
    }

    public Double getPrice() {
        return getCurrentProcedureValue("price");
    }

    /**
     * 条件基值
     **/
    public void setBaseValue(Double value) {
        getCurrentProcedure().put("baseValue", value);
    }

    public Double getBaseValue() {
        return getCurrentProcedureValue("baseValue");
    }

    /**
     * 定价值
     **/
    public void setPricingValue(Double value) {
        getCurrentProcedure().put("value", value);
    }

    public Double getPricingValue() {
        return getCurrentProcedureValue("value");
    }

    /**
     * 条件记录
     **/
    public void setConditionRecord(Object entity) {
        getCurrentProcedure().put("conditionRecord", entity);
    }

    public Object getConditionRecord() {
        return getCurrentProcedureValue("conditionRecord");
    }

    private <T> T getCurrentProcedureValue(String key) {
        if (currentProcedure == null) return null;
        if (!currentProcedure.containsKey(key)) return null;
        return (T) currentProcedure.get(key);
    }

    public void set(String k, Object v) {
        result.put(k, v);
    }

    public Object get(String k) {
        return result.get(k);
    }
}
